<?php
/**
 *需要权限验证、需要登录验证公共基础类
 * Date: 2021/10/13
 */
namespace app\api\controller;
use app\common\model\redis\RedisModel;
class AuthBase  extends Base
{
    public $userId = 0;
    public $userNumber = "";
    //当前请求的token
    public $xToken = "";
    public function initialize()
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: X-Token,Content-Type, Content-Length, Authorization, Accept, X-Requested-With, Current-Page');
        header('Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE,PUT');
        parent::initialize();
        $this->xToken = $this->request->header(config("config.head.token"));
        if(!$this->xToken || !$this->isLogin()){
            return $this->returnResult(config("config.code.not_login"),'token错误！');
        }
    }

    /**
     * 判断用户是否已鉴权
     * @return bool
     */
    public function isLogin(): bool
    {
        $res  = $this->getTokenInfo($this->xToken);
        if($res){
            return true;
        }
        return false;
    }


    /**
     * 通过token去鉴权
     */
    public function getTokenInfo($token){
        $token = \app\common\lib\Huisheng::getToken($token);
        if($token){
            return $token['token'];
        }else{
            return false;
        }
    }
}
